﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class student_chaxun : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    string Constr = "Server=(local);Integrated Security=true;DataBase=tsmanager;";//采用windows身份验证方式
    protected void Button2_Click(object sender, EventArgs e)
    {
        string type = DropDownList1.SelectedItem.Text.ToString();
        string sech = "%" + TextBox1.Text.Trim() + "%";
       
        SqlConnection con = new SqlConnection(Constr);
        string strcom;
       
        try
        {
            con.Open();

            if (TextBox1.Text == null)
            {
                strcom = "select * from BookInformation where BI_TYPE='" + type + "'";
            }
            else
            {
                strcom = "select * from BookInformation where BI_TYPE='" + type + "' and BI_TITLE like '" + sech + "'";
            }
            SqlDataAdapter SDA = new SqlDataAdapter(strcom, con);
            DataSet dataset = new DataSet();
            SDA.Fill(dataset, "BookInformation");
            griview.DataSource = dataset.Tables["BookInformation"].DefaultView;
            griview.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }

    string bookkc;
    string bookjc;
    protected void selectdata(string bookid) { 
        SqlConnection con = new SqlConnection(Constr);
        con.Open();
        string strcom = "select * from BookInformation where BI_BID='" + bookid + "'";
        SqlCommand com = new SqlCommand(strcom,con);
        try
        {
            SqlDataReader sdr = com.ExecuteReader();
            if(sdr.Read()){
                bookkc = sdr["BI_NOB"].ToString();
            }else{
                Response.Write("<script language=javascript>alert('查询失败！')</script>");
            }
            
        }
        catch (Exception ex)
        {
            
        }
    
    }

   protected void updata(string bookid) {
        SqlConnection con = new SqlConnection(Constr);
        con.Open();
        bookkc=Convert.ToString(Convert.ToInt32(bookkc)-1);
        string strcom = "update BookInformation set BI_NOB='" + bookkc + "' where BI_BID='" + bookid + "'";
        SqlCommand com = new SqlCommand(strcom, con);
        try
        {
            com.ExecuteNonQuery();
            Response.Write("<script language=javascript>alert('更新成功！')</script>");
        }
        catch (Exception ex)
        {
            Response.Write(ex);
            Response.Write("<script language=javascript>alert('更新失败！')</script>");
        }
        
    }


    protected void Button3_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow dgi in griview.Rows)
        {
            CheckBox cb = (CheckBox)dgi.FindControl("CheckBox1");
            if (cb.Checked == true)
            {
                SqlConnection con = new SqlConnection(Constr);
                con.Open();
                string strcom = "insert into JHbooks(BARB_LDATE,BARB_DDATE,BARB_BDATE,ReadersID,BI_BID) values('" + DateTime.Now.ToShortDateString().ToString() + "','" + DateTime.Now.AddDays(30).ToString() +"','" + 30 + "','" + Session["id"].ToString() + "','" + dgi.Cells[1].Text.Trim() + "')";
                SqlCommand com = new SqlCommand(strcom,con);
                try
                {
                    com.ExecuteNonQuery();
                    selectdata(dgi.Cells[1].Text.Trim());
                    updata(dgi.Cells[1].Text.Trim());
                    Response.Write("<script language=javascript>alert('借阅成功！')</script>");
                }catch(Exception ex){
                    Response.Write("<script language=javascript>alert('借阅失败！')</script>");
                    Response.Write(ex);
                }
            }
        }
    }
    protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

    }
}